﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Flagwind.Data
{
	internal static class SqlInfoManager
	{
		#region 私有字段

		private static Dictionary<string, SqlInfo> _infos;
		private readonly static object _lock = new object();

		#endregion

		#region 私有属性

		private static Dictionary<string, SqlInfo> Infos
		{
			get
			{
				if(_infos == null)
				{
					lock(_lock)
					{
						if(_infos == null)
						{
							var temp = new Dictionary<string, SqlInfo>();
							_infos = temp;
						}
					}
				}
				return _infos;
			}
		}

		#endregion

		#region 公共方法

		/// <summary>
		/// 从缓存中获取项。
		/// </summary>
		/// <param name="key"></param>
		/// <returns></returns>
		public static SqlInfo Get(string key)
		{
			SqlInfo value = null;
			_infos.TryGetValue(key, out value);
			return value;
		}

		/// <summary>
		/// 增加一项到缓存中。
		/// </summary>
		/// <param name="key"></param>
		/// <param name="info"></param>
		/// <returns></returns>
		public static bool Add(string key, SqlInfo info)
		{
			SqlInfo value = Get(key);
			if(value != null)
			{
				return false;
			}
			lock(_lock)
			{
				_infos.Add(key, info);
			}
			return true;
		}

		#endregion
	}
}